﻿<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<dynamic>" %>

<%@ Import Namespace="EzDesk.Utility.Json" %>
<asp:Content ID="Content2" ContentPlaceHolderID="PageTitle" runat="server">
    <title>项目计划</title>
</asp:Content>
<asp:Content ID="Content3" ContentPlaceHolderID="PageStyles" runat="server">
    <link href="../../Content/project/project.css" rel="stylesheet" type="text/css" />
    <link href="../../Content/project/project.workitemlist.css" rel="stylesheet" type="text/css" />
    <link href="../../Content/ui.button.css" rel="stylesheet" type="text/css" />
</asp:Content>
<asp:Content ID="Content4" ContentPlaceHolderID="PageScripts" runat="server">
    <script src="../../Scripts/Project/projectTab.js" type="text/javascript"></script>
    <script type="text/javascript">
        var currentProject;
        function CreateAccessor() {
            //实现服务器交互方法
            EzAccessor = {
                "getProject": function() {
                    return EzDesk.Service.BuildReference(<%= WebJsonExtension.ToJson(Model.ProjectInfo) %>);  
                }
            };                
        }

        $(function() {
            CreateAccessor();
            
            currentProject = EzAccessor.getProject();
            
            $('#tabs').projectTab(currentProject, 'link-tasks');
            
            var projectId = currentProject.Id;

            if (ProjectInfo) {
                ProjectInfo.Show(currentProject);
            }
        });
    </script>
    <link href="../../Scripts/Project/dhtmlxgantt.css" rel="stylesheet" type="text/css" />
    <script src="../../Scripts/Project/dhtmlxcommon.js" type="text/javascript"></script>
    <script src="../../Scripts/Project/dhtmlxgantt.js" type="text/javascript"></script>
    <script src="../../Scripts/Project/dhtmlxgantt.ext.js" type="text/javascript"></script>
    <script src="../../Scripts/Project/TestData.js" type="text/javascript"></script>
    <textarea id="taskTemplate" style="display: none">
        <div id="{$T.Id}">
            <ul class="fieldslist">
            <li class="l">名称:</li>
            <li class="r"><input type="text" class="txt-name" value="{$T.Name}"/></li>
            <li class="l">负责人:</li>
            <li class="r"><select class="select-director"></select></li>
            <li class="l">开始时间:</li>
            <li class="r"><input type="text" class="txt-est" value="{$T.EST}"/></li>
            <li class="l">工时:</li>
            <li class="r"><input type="text" class="txt-duration" value="{$T.Duration}"/>小时</li>
            <li class="l">前置任务:</li>
            <li class="r"><select class="select-pretask" /></li>
            <li class="l"></li>
            <li class="r"><button class="btn-submit">添加</button></li>
            </ul>
        </div>
    </textarea>
    <script>
        var ganttChartControl;
        $(function () {
            GanttLoad(EzAccessor.getProject());
        });

        function GanttLoad(project) {
            ganttChartControl = new GanttChart();
            ganttChartControl.loadDataIn('GanttDiv', project ? project : EzAccessor.getProject());
            ganttChartControl.AddEventListener("TaskChanged", OnTaskChanged);
            ganttChartControl.AddEventListener("ShowWorkitem", ShowWorkitemList);
        }

        function OnTaskChanged(taskInfo) {
            var actionMethodMapping = { "Create": AddNewTask, "Delete": DeleteTask, "Change": UpdateTask };
            actionMethodMapping[taskInfo.ClientAction](taskInfo);
        }
        function AddNewTask(taskInfo) {
            var task = new Task(taskInfo);
            task.ProjectId = currentProject.Id;
            task.Id = "00000000-0000-0000-0000-000000000000";
            EzDesk.Service.AddNewTask_Sync(function (e) {
                var result_task = e.Result;
                taskInfo.Id = result_task.Id;
            }, { 'projectId': currentProject.Id, 'taskStr': $.toJSON(task) });
        }
        function UpdateTask(taskInfo) {
            var task = new Task(taskInfo);
            task.ProjectId = currentProject.Id;
            EzDesk.Service.UpdateTask(function (e) {
                //dosomething
            }, { 'taskStr': $.toJSON(task) });
        }
        function DeleteTask(taskInfo) {
            var task = new Task(taskInfo);
            task.ProjectId = currentProject.Id;
            EzDesk.Service.DeleteTask(function (e) {
                //dosomething
            }, { 'taskId': task.Id });
        }
        function ShowWorkitemList(taskInfo) {
            var convertor = [" ", "内容-计划", "到期", "负责人", "操作"];
            if (taskInfo && taskInfo != null)
                convertor  = [" ", "内容", "到期", "负责人", "操作"];

            var args = {
                myConvertor: convertor,
                GetWorkitemListService: getWorkItemListService,
                initCallback: function () {
                    $("#popup-workitemlist-container-1").dialog({
                        height: "auto",
                        width: 600,
                        title: "工作任务",
                        modal: true,
                        position: ["center", 200]
                    });
                }
            };
            function getWorkItemListService(callback) {
                currentProject = EzDesk.Service.GetFullProjectById_Sync(null, { id: currentProject.Id });
                if (taskInfo && taskInfo != null) {
                    var task = null;
                    for (var q in currentProject.Task) {
                        if (currentProject.Task[q].Id == taskInfo.Id) {
                            task = currentProject.Task[q];
                            break;
                        }
                    }
                    callback(task.Workitem);
                }
                else
                    callback(currentProject.Workitem);
            }
            $('#divWorkitems').workitemList(args);
        }
    </script>
    <script type="text/javascript">
        $(function () {

            $(window).wresize(function () {
                if (currentProject && currentProject.Id) {
                    GanttLoad(EzDesk.Service.GetFullProjectById_Sync(undefined, { id: currentProject.Id }));
                }
            });

            //初始化修改起始时间功能
            $(".btn-save-word")
            .button({
                icons: {
                    primary: "ui-icon-disk"
                },
                text: true
            });


            $('#btn-change-startOn').click(function () {
                var $panelChangeStartOn = $('#panel-change-startOn')
                $panelChangeStartOn.datepicker({
                    changeMonth: true,
                    numberOfMonths: 2,
                    onSelect: function (dateText, inst) {
                        var startOnDate = $.datepicker.parseDate('yy-mm-dd', dateText).toEpoch();

                        EzDesk.Service.ChangeProjectStartOn_Sync(null,{ projectId: currentProject.Id,startOn: startOnDate});
                        EzDesk.Service.GetFullProjectById(
                            function (e) {
                                GanttLoad(e);
                            },
                            { id: currentProject.Id }
                        );

                        $panelChangeStartOn.fadeOut();
                    }
                });

                $panelChangeStartOn.fadeIn();
                $panelChangeStartOn.mouseleave(function () {
                    $panelChangeStartOn.fadeOut();
                });
                var orignalLeft = $('#btn-change-startOn').offset().left;
                $panelChangeStartOn.css({ left: orignalLeft });
            });


            //初始化保存为模板的功能



            $("#btn-save-projectTemplate").click(function () {
                $("#create-projectTemplate").dialog({ title: "创建项目模板", width: 600, height: 200, modal: true });
            });

            $("#btn-ok-createProjectTemplate").click(function () {
                var projectTemplate = {};
                projectTemplate.Name = $('#txt-projectTemplateName').val();
                projectTemplate.TemplateData = $.toJSON(ganttChartControl.getProject());
                EzDesk.Service.CreateProjectTemplate(function (e) { }, { 'projectTemplateStr': $.toJSON(projectTemplate) });
                $("#create-projectTemplate").dialog("close");
            });
        });
    </script>
</asp:Content>
<asp:Content ID="Content5" ContentPlaceHolderID="PageHead" runat="server">
    <%
        Html.RenderPartial("_sayHello"); %>
</asp:Content>
<asp:Content ID="Content7" ContentPlaceHolderID="PageContent" runat="server">
    <div id="page-head" class="page-head">
        <div id="links">
            <a href="/project/ProjectList" class="head">返回项目列表</a>
        </div>
        <% Html.RenderPartial("_projectInfo"); %>
        <div id="tabs">
        </div>
    </div>
    <div id="page-body">
        <div>
        <div >
        <div style="display:block;" id="div-add-workitem">
                <a id="btn-change-startOn">
                    <span class="ui-icon ui-icon-pencil" style="display:inline-block;"></span>
                    变更项目起始时间
                </a>
                <div id="panel-change-startOn" style="position: absolute; float: left; z-index: 10002;">
                </div>
                <a id="btn-save-projectTemplate"><span class="ui-icon ui-icon-disk" style="display:inline-block;"></span>保存为模板</a>
                <div id="create-projectTemplate" style="display: none;">
                    <ul class="fieldslist">
                        <li>
                            <div class="l">
                                <label>
                                    模板名称：
                                </label>
                            </div>
                            <div class="r">
                                <input class="txtName text" id="txt-projectTemplateName" />
                                <div id="btn-ok-createProjectTemplate" class="btn-save-word">
                                    保存模板</div>
                            </div>
                        </li>
                    </ul>
                </div>
            </div>
        </div>
        </div>
        <div style="width: 100%; height: 460px; position: relative" id="GanttDiv">
        </div>
        <div id="log"></div>
        <div class="content">
            <div id="popup-workitemlist-container-1" class="content-section" style="display: none;">
                <div id="divWorkitems" class="tableContainer">
                </div>
            </div>
        </div>

    </div>
</asp:Content>
<asp:Content ID="Content9" ContentPlaceHolderID="PageFoot" runat="server">
    <div id="tip_Remind" style="display: none;">
        <h1>
            小贴士:</h1>
        <p class="indent">
        </p>
    </div>
</asp:Content>
